package cn.hahah.module.blog.controller.admin.article;

import cn.hahah.module.blog.dal.dataobject.articletag.ArticleTagDO;
import cn.hahah.module.blog.dal.dataobject.articletype.ArticleTypeDO;
import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.articletype.ArticleTypeService;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.article.vo.*;
import cn.hahah.module.blog.dal.dataobject.article.ArticleDO;
import cn.hahah.module.blog.service.article.ArticleService;

@Tag(name = "管理后台 - 博客系统文章")
@RestController
@RequestMapping("/blog/article")
@Validated
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @Resource
    private UsersService usersService;

    @Resource
    private ArticleTypeService articleTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建博客系统文章")
    @PreAuthorize("@ss.hasPermission('blog:article:create')")
    public CommonResult<Long> createArticle(@Valid @RequestBody ArticleSaveReqVO createReqVO) {
        return success(articleService.createArticle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新博客系统文章")
    @PreAuthorize("@ss.hasPermission('blog:article:update')")
    public CommonResult<Boolean> updateArticle(@Valid @RequestBody ArticleSaveReqVO updateReqVO) {
        articleService.updateArticle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除博客系统文章")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:article:delete')")
    public CommonResult<Boolean> deleteArticle(@RequestParam("id") Long id) {
        articleService.deleteArticle(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得博客系统文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:article:query')")
    public CommonResult<ArticleRespVO> getArticle(@RequestParam("id") Long id) {
        ArticleDO article = articleService.getArticle(id);
        return success(BeanUtils.toBean(article, ArticleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得博客系统文章分页")
    @PreAuthorize("@ss.hasPermission('blog:article:query')")
    public CommonResult<PageResult<ArticleRespVO>> getArticlePage(@Valid ArticlePageReqVO pageReqVO) {
        PageResult<ArticleDO> pageResult = articleService.getArticlePage(pageReqVO);
        PageResult<ArticleRespVO> result = BeanUtils.toBean(pageResult, ArticleRespVO.class);
        for (ArticleRespVO articleRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(articleRespVO.getUserId());
            ArticleTypeDO articleTypeDO = articleTypeService.getArticleType(articleRespVO.getTypeId());

            if(usersDO != null){
                articleRespVO.setUserName(usersDO.getUsername());
            }
            if(articleTypeDO != null){
                articleRespVO.setTypeName(articleTypeDO.getName());
            }
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出博客系统文章 Excel")
    @PreAuthorize("@ss.hasPermission('blog:article:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleExcel(@Valid ArticlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ArticleDO> list = articleService.getArticlePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "博客系统文章.xls", "数据", ArticleRespVO.class,
                        BeanUtils.toBean(list, ArticleRespVO.class));
    }

}